package edu.chl.grupp11.aegas.view;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

import org.swixml.SwingEngine;

import edu.chl.grupp11.aegas.model.AbstractConfig;
import edu.chl.grupp11.aegas.model.ConfigFactory;

public class MainView extends Observable implements ActionListener, Observer {
	public static JLabel errorLabel;

	public static void shutDownApplication() {
		SwingEngine.getAppFrame().dispose();
		System.exit(0);
	}

	private ResourceBundle bundle;
	private JButton openFileButton;
	private JButton openFolderButton;
	private JButton openURLButton;
	private JButton saveButton, clearAllButton, cancelButton;
	private JPanel fileList;
	private JPanel editPanel;
	private JPanel filePanel;
	private JPanel tagList;
	private JMenuItem openFileMenuItem, openFolderMenuItem, openURLMenuItem,
			aboutMenuItem, settingsMenuItem, exitMenuItem, helpMenuItem,
			langPluginMenuItem, saveMenuItem, cancelMenuItem,
			extensionMenuItem;
	private JMenu fileMenu, toolsMenu, helpMenu, languageMenu;
	private JRadioButtonMenuItem englishRadioButton;
	private ButtonGroup languageButtonGroup;
	private SwingEngine swix;

	private String language;

	private AbstractConfig config = ConfigFactory.getSharedConfig();

	/** Renders UI at construction */

	public MainView() {
		swix = new SwingEngine(this);
		try {
			swix.render("edu/chl/grupp11/aegas/view/xml/XmlView.xml");
			swix.getRootComponent().setVisible(true);
			findPropertyFiles();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		bundle = config.getResourceBundle();
		setLanguage("English");
		updateText();
		englishRadioButton.addActionListener(this);
		englishRadioButton.setActionCommand("CHANGE_LANG");
		langPluginMenuItem.setActionCommand("ADD_PLUGIN");
		englishRadioButton.setSelected(true);
		openFileButton.addActionListener(this);
		openFolderButton.addActionListener(this);
		openURLButton.addActionListener(this);
		saveButton.addActionListener(this);
		openFileMenuItem.addActionListener(this);
		openFolderMenuItem.addActionListener(this);
		openURLMenuItem.addActionListener(this);
		aboutMenuItem.addActionListener(this);
		helpMenuItem.addActionListener(this);
		settingsMenuItem.addActionListener(this);
		clearAllButton.addActionListener(this);
		exitMenuItem.addActionListener(this);
		saveMenuItem.addActionListener(this);
		cancelButton.addActionListener(this);
		cancelMenuItem.addActionListener(this);
		langPluginMenuItem.addActionListener(this);
		extensionMenuItem.addActionListener(this);
		setAccelerators();

		SwingEngine.getAppFrame().addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				shutDownApplication();
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		setChanged();
		notifyObservers(e);
	}

	public void addLanguage(String bundleName) {
		if (bundleName != null) {
			setLanguage(bundleName);
			if (!bundleName.equals("English")) {
				JRadioButtonMenuItem newLanguage = new JRadioButtonMenuItem(
						bundleName);
				languageMenu.add(newLanguage);
				newLanguage.addActionListener(this);
				newLanguage.setActionCommand("CHANGE_LANG");
				languageButtonGroup.add(newLanguage);
				newLanguage.setSelected(true);
			}
		}
	}

	private void findPropertyFiles() throws IOException {
		File folder = new File("bin/edu/chl/grupp11/aegas/recources/bundles");
		for (File f : folder.listFiles()) {
			if (f.isFile()
					&& f.getName().substring(f.getName().lastIndexOf('.') + 1)
							.equalsIgnoreCase("properties")) {
				addLanguage(f.getName().substring(0,
						f.getName().lastIndexOf('.')));
			}
		}
	}

	public ResourceBundle getBundle() {
		return bundle;
	}

	public JPanel getEditPanel() {
		return editPanel;
	}

	public JPanel getFileList() {
		return fileList;
	}

	public JPanel getFilePanel() {
		return filePanel;
	}

	public String getLanguage() {
		return this.language;
	}

	public JPanel getTagList() {
		return tagList;
	}

	private void setAccelerators() {
		KeyStroke openAcc = KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit
				.getDefaultToolkit().getMenuShortcutKeyMask());
		openFileMenuItem.setAccelerator(openAcc);
		KeyStroke saveAcc = KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit
				.getDefaultToolkit().getMenuShortcutKeyMask());
		saveMenuItem.setAccelerator(saveAcc);

	}

	public void setLanguage(String language) {
		this.language = language;
		setChanged();
		notifyObservers();
	}

	@Override
	public void update(Observable o, Object arg) {
		bundle = config.getResourceBundle();
		updateText();
	}

	private void updateText() {
		// File menu
		fileMenu.setText(bundle.getString("file"));
		saveMenuItem.setText(bundle.getString("save"));
		cancelMenuItem.setText(bundle.getString("cancel"));
		exitMenuItem.setText(bundle.getString("close"));
		openFileMenuItem.setText(bundle.getString("open_File"));
		openFolderMenuItem.setText(bundle.getString("open_Folder"));
		openURLMenuItem.setText(bundle.getString("open_URL"));
		// Tools menu
		toolsMenu.setText(bundle.getString("tools"));
		languageMenu.setText(bundle.getString("language"));
		settingsMenuItem.setText(bundle.getString("settings"));
		langPluginMenuItem.setText(bundle.getString("add_lang_Plugin"));
		extensionMenuItem.setText(bundle.getString("add_exst"));
		// Help menu
		helpMenu.setText(bundle.getString("help"));
		aboutMenuItem.setText(bundle.getString("about"));
		helpMenuItem.setText(bundle.getString("help"));
		// Buttons
		saveButton.setText(bundle.getString("save"));
		cancelButton.setText(bundle.getString("cancel"));
		openFileButton.setToolTipText(bundle.getString("open_File"));
		openURLButton.setToolTipText(bundle.getString("open_URL"));
		openFolderButton.setToolTipText(bundle.getString("open_Folder"));
		clearAllButton.setText(bundle.getString("clear_All"));
	}
}
